import 'package:huxiaodui/api/api.dart';
import 'package:huxiaodui/api/model/unit_model.dart';
import 'package:huxiaodui/components/UnitsComponent.dart';
import 'package:flutter/material.dart';

class UnitListPage extends StatefulWidget {
  const UnitListPage({super.key});

  @override
  UnitListPageState createState() => UnitListPageState();
}

class UnitListPageState extends State<UnitListPage> {
  TextEditingController controller = TextEditingController();

  String lastInfo = "";
  List<UnitModel> units = [];

  Future<void> getUnits() async {
    await Api.request(
      isToast: true,
      url: UnitUrl.all,
      data: {},
      success: (res) async {
        setState(() {
          units = UnitModel.toList(res.data);
        });
      },
    );
  }

  @override
  void initState() {
    super.initState();
    getUnits();
  }

  @override
  Widget build(BuildContext context) {
    Widget getInput() {
      return Container(
        width: 200,
        height: 45,
        padding: const EdgeInsets.only(left: 5),
        decoration: BoxDecoration(
          color: Colors.white.withAlpha(900),
          borderRadius: BorderRadius.circular(10),
        ),
        child: TextField(
          controller: controller,
          decoration: InputDecoration(
            border: InputBorder.none,
            hintStyle: TextStyle(
              color:
                  Theme.of(context).textTheme.bodyMedium!.color!.withAlpha(500),
            ),
            hintText: "搜索",
            suffixIcon: IconButton(
              onPressed: () {
                if (controller.text.isNotEmpty) getUnits();
              },
              icon: const Icon(Icons.search),
            ),
          ),
        ),
      );
    }

    return Scaffold(
      appBar: AppBar(
        title: const Text("所有单位"),
      ),
      body: RefreshIndicator(
        onRefresh: () async {
          getUnits();
        },
        child: UnitsComponent(
          units: units,

        ),
      ),
    );
  }
}
